<template>
	<view class="container">
		<view class="recordList">

			<view class="recordItem" v-for="item in teamList" :key="item.uid">
				<view class="refundHd">推广码：{{item.spread_code}} <span @click="copyText(item.spread_code)" class="copy">复制</span></view>
				<view class="con">
					<view class="price">名称：￥{{item.name}}</view>
					<view class="num">等级：{{item.level_name}}</view>
				</view>
				<view class="footer">
					<view class="btns">
						<view class="btn" @click="showTransfer(item.spread_code)">划拨</view>
					</view>
				</view>
			</view>

		</view>
		<u-loadmore :status="status" />

		<u-modal v-model="transferShow" title="划拨库存" :show-cancel-button="true" confirm-text="划拨"
			confirm-color="#20cbd4" @confirm="transfer" @cancel="cancelTransfer">
			<view class="slot-content">
				<u-field class="contentItem" v-model="spread_code" :disabled="true" label="客户编码"
					placeholder="请填写客户编码"></u-field>
				<u-field class="contentItem" v-model="transferPrice" :disabled="true" type="text" label="单价"
					placeholder=" "></u-field>
				<u-field class="contentItem" v-model="num" type="number" label="数量" placeholder="请填写数量"></u-field>
			</view>
		</u-modal>
		
		<u-modal v-model="showConfirm" title="确定要划拨吗？" :show-cancel-button="true" :content="msg" @cancel="confirmCancel"
			@confirm="confirmTransfer"></u-modal>
	</view>
</template>

<script>
	import {
		stockTransfer,
		stockCheckPrice,
		stockTeamList
	} from "@/common/api.js";
	export default {
		components: {},
		data() {
			return {
				showConfirm: false,
				transferShow: false,
				transferPrice: '',
				transferGoodsPrice: '',
				page: 1,
				num: '',
				status: 'loadmore',
				spread_code: '',
				teamList: []
			};
		},
		onLoad() {
			this.selectOrder()
		},
		onReachBottom() {
			this.selectOrder()
		},
		methods: {
			checkPrice() {
				if (this.spread_code && this.spread_code.length > 6) {
					stockCheckPrice(this, {
						spread_code: this.spread_code
					}, (res) => {
						this.transferGoodsPrice = res
						this.transferPrice = '￥' + res
					});
				}
			},
			cancelTransfer() {
				this.spread_code = ''
				this.transferPrice = ''
				this.transferGoodsPrice = ''
				this.num = ''
			},
			showTransfer(spread_code){
				this.transferShow = true
				this.spread_code = spread_code
				this.checkPrice()
			},
			transfer() {
				let price = this.transferGoodsPrice * this.num
				price = price.toFixed(2)
				let msg = '客户编码：' + this.spread_code + '，划拨数量：' + this.num + '，总价：￥' + price
				this.msg = msg
				this.showConfirm = true
			},
			selectOrder() {
				if (this.status !== 'loadmore') {
					return
				}
				this.status = 'loading'
				stockTeamList(this, {
					page: this.page
				}, (res) => {
					this.$set(this, 'teamList', [...this.teamList, ...res])
					this.page++
					this.status = 'loadmore'
					if (res.length === 0) {
						this.status = 'nomore'
					}
				})
			},
			confirmCancel() {
			
			},
			confirmTransfer() {
				stockTransfer(this, {
					spread_code: this.spread_code,
					num: this.num
				}, (res) => {
					this.spread_code = ''
					this.num = ''
					this.transferPrice = ''
					this.transferGoodsPrice = ''
					this.$u.toast('划拨成功');
				})
			},
			copyText(spread_code){
				uni.setClipboardData({
					data:spread_code
				})
			},
		}
	};
</script>
<style lang="scss">
	page {
		font-family: PingFang SC;
		background-color: #F6F6F6;
		height: 100%;
	}

	.recordItem {
		margin: 15rpx 15rpx 25rpx 15rpx;
		border-radius: 15rpx;
		background-color: #FFFFFF;
		padding: 15rpx;
		position: relative;

		.con {
			.source {
				height: 60rpx;
				line-height: 60rpx;
			}

			.num {
				font-size: 12px;
				color: #999999;
				height: 40rpx;
				line-height: 40rpx;
			}

			.time {
				font-size: 12px;
				color: #999999;
				height: 40rpx;
				line-height: 40rpx;
			}
		}

		.footer {
			border-top: 1rpx solid #efefef;
			color: #999999;
			line-height: 60rpx;
			text-align: right;
			padding: 15rpx;

			.status {
				float: left;
				color: #333333;
			}

			.amount {
				overflow: hidden;
			}

			.btns {
				overflow: hidden;
			}

			.btn {
				font-size: 12px;
				border: 1rpx solid #d5d5d5;
				display: inline-block;
				border-radius: 10rpx;
				float: right;
				margin-left: 20rpx;
				padding: 0 15rpx;
				line-height: 50rpx;
			}
		}
	}
	.copy{
		margin-left: 30rpx;
	}
</style>